package com.addzero.web.ui.hooks.table.generic.drawer

import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.addzero.web.ui.components.drawer.DrawerDirection
import com.addzero.web.ui.components.drawer.GenericDrawer
import com.addzero.web.ui.components.drawer.UseGenericDrawer
import com.addzero.web.ui.hooks.UseHook
import com.addzero.web.ui.hooks.form.UseDynamicForm
import com.addzero.web.ui.hooks.table.table.UseTableContent

class UseFormDrawer<E : Any>(
  val  title:String,
  val  useTableContent: UseTableContent<E>,
  val  onFormSubmit: (E) -> Unit,
  val  columnCount: Int = 2,
  val  direction: DrawerDirection = DrawerDirection.LEFT,
  val  width: Int = 500,
  val  height: Int = 600
): UseHook<UseFormDrawer<E>> {
    override val render: @Composable (() -> Unit)
        get() = {


            val useDynamicForm = remember {
                UseDynamicForm(useTableContent.columns, columnCount).apply {
                    formItem = useTableContent.currentSelectItem
                }
            }

            val useGenericDrawer = UseGenericDrawer(
                direction = direction,
                title = title,
                width = width,
                height = height,
                withBackdrop = true,
                content ={
                  useDynamicForm.render{}
                }
            )

            // 创建通用抽屉
            val drawerViewModel = GenericDrawer(
                direction = direction,
                title = "编辑",
                width = width,
                height = height,
                content = {
                    Box(modifier = Modifier.padding(16.dp)) {
                        useDynamicForm.render{}
                    }
                }
            )

            // 监听表单显示状态
            if (useTableContent.showFormFlag) {
                drawerViewModel.show()
            } else {
                drawerViewModel.hide()
            }

            // 当抽屉关闭时，更新表单标志
            if (!drawerViewModel.isExpanded && useTableContent.showFormFlag) {
                useTableContent.showFormFlag = false
            }

            // 添加提交和取消按钮
            // 注意：这里可以在UseDynamicForm中添加按钮区域，或者在抽屉底部添加固定按钮栏


        }

}

/**
 * 表单抽屉组件 - 替代FormDialog
 */
@Composable
@Preview
fun <E : Any> FormDrawer( ) {
    Text("dasiodjosja")

}

/**
 * 替换UseTable中的FormDialog调用
 * 在UseTable.kt中：
 *
 * 将
 * FormDialog(useTableContent, onFormSubmit = { item ->
 *     withRefresh { onSave(item) }
 * })
 *
 * 替换为
 * FormDrawer(useTableContent, onFormSubmit = { item ->
 *     withRefresh { onSave(item) }
 * })
 */
